/**
 * Created by Administrator on 2015/12/29.
 */
angular.module('ywkh.service',[])
  .service('ywkhService',['$http','$q','$timeout','$localstorage','appItfAopService','$ionicPopup','$ionicLoading',function($http,$q,$timeout,$localstorage,appItfAopService,$ionicPopup,$ionicLoading){
   var params = {
     "step1":'',
     "step2":'',
     "step3":"",
     "step3_1":"",
     "step3_2":"",
     "step4":"",
     "mac":"",
     "CustList":{},
     "ConTel":"",
     "val_tclx":"",//用户选择的套餐类别 2G,3G,4G
     "MobilePhone":"",//手机号码,
     "searchUserInfoByPhone":{"mobilePhone": ""},
     "ICCIDNum":"",//SIM卡号（ICCID）
     "QueryUserBySimParams":{
       "QryType":"",  //查询类别，1- 根据SIM查号码，2 - 根据号码查SIM，必填
       "QryCode":"", //查询类别为SIM时，该参数为号码，查询类别为号码时，该参数为SIM，必填
       "IsPre":"0", //是否为预配号码。0已开卡（返档使用）；1预配卡（开卡使用），必填
     },
     'NummoreData':true,
     'DevelopmoreData':true,
     "BlueSimCard":{},//蓝牙读卡的数据
     "BarSimCard":{"text":""},//手机扫描的数据
     "IdCard":{"address":""},//身份证读取的数据
     "PhoneNum":{},
     "PhoneNumList":[],//用户的选择手机号
     "Developer":{},//用户选择的发展人
     "DeveloperList":[],//所有发展人
     "Product":{},//用户选择的单个产品
     "ProductList" :[//所有的产品
    /* {'proId':'01','proName':'2G套餐--手机无限时上网套餐26元'},{'proId':'02','proName':'2G套餐--全国漫游通话套餐46元'},
     {'proId':'03','proName':'3G套餐--省内流量不限时套餐66元'},{'proId':'04','proName':'3G套餐--国内流量套餐86元'},
     {'proId':'05','proName':'01-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'02-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'03-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'04-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'05-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'06-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'07-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'08-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'09-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'10-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'11-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'12-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'13-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'14-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'15-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'16-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'17-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'18-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'19-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'20-4G套餐--省内4G加量不加价126元'},
     {'proId':'05','proName':'21-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'22-4G套餐--省内4G加量不加价126元'}*/
   ],
     "HuoType":[{"htid":"01","htname":"4G套餐大活动"},{"htid":"02","htname":"3G套餐中活动"},{"htid":"03","htname":"2G套餐小活动"}],
     "HuoList":[],
     "validCustParams":{"sysCode":"","psptType":"","brandCode":""}
     }

    return {
      getParams:function() {
        return params;
      },
      ReadIdCard :function(param){
        //ios插件真心无语,必须要先去搜索,然后匹配,然后读卡,每次操作都要这样
        /*设备列表刷新*/
        var options = {};
        options.deviceType = ['2', '4', '8'];
        options.name=param;
        console.log('设备列表扫描:' + JSON.stringify(options));

        $ionicLoading.show({
          template: '<ion-spinner icon="ios"></ion-spinner>',
          content: '拼命加载中，请耐心等候...',
          animation: 'fade-in',
          showBackdrop: false,
          maxWidth: 200,
          showDelay: 0
        });

        //搜索蓝牙设备,可以直接调用搜索的就可以,
        BLEIDCardPlugin.ai_refreshDeviceList(function (result) {
          console.log('扫描到的设备列表如下:' + JSON.stringify(result) + '设备个数:' + result.devices.length);
          $timeout(function(){
          },1000);
        }, function (error) {
          console.error('设备扫描失败' + JSON.stringify(error));
          $ionicLoading.hide();
        }, JSON.stringify(options));

        //匹配蓝牙设备
        BLEIDCardPlugin.ai_connectDevice(function (result) {
          console.log('读卡器连接成功');
          console.log('混合界面开始读取证件:' + JSON.stringify(options));
          BLEIDCardPlugin.ai_readIDCard(function (result) {
            console.log('读取到的证件信息如下:' + JSON.stringify(result));
            params.step1=1;//下一步按钮可用
            //params.IdCard=result;

            //在这里将数据封装成森锐的数据 不用大面积更改开户使用的原森锐参数
            params.IdCard.address=result.Address;
            params.IdCard.authority=result.IssuedAt;
            params.IdCard.avatar=result.Avatar;
            params.IdCard.birth=result.Birth;
            params.IdCard.cardNo=result.CardNo;
            params.IdCard.ethnicity=result.Nation;
            params.IdCard.name=result.Name;
            params.IdCard.period=result.EffectedDate+"-"+result.ExpiredDate;
            params.IdCard.sex=result.Sex;

            console.log(angular.toJson(params.IdCard.Address));
            $ionicLoading.hide();
          }, function (error) {
            console.log('读取证件信息失败:' + JSON.stringify(error));
            $ionicLoading.hide();
            params.step1=0;//下一步按钮禁用
          }, JSON.stringify(options));

        }, function (error) {
          console.log('读卡器连接失败');
          $ionicLoading.hide();
        }, JSON.stringify(options));

          /*bluetooth.sr_lydk(param,function(success){
              $ionicLoading.hide();
              $timeout(function(){
                params.IdCard=angular.fromJson(success);
              },1000);
              params.step1=1;
            },function(error){
              $ionicLoading.hide();
              var alertPopup=$ionicPopup.alert({
                title:'读取失败',
                template:angular.toJson(error),
                okText: '确定', // String (默认: 'OK')。OK按钮的文字。
                okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
              });
              params.step1=0;
            }
          )*/
        return params.IdCard;
      },
      //客户信息认证
      Authenticate:function(param){
        return appItfAopService.do_authenticate_req(param);
      },
      getProductList:function(param){
/*        if('2G'==type){params.ProType=[{'proId':'01','proName':'2G套餐--手机无限时上网套餐26元'},{'proId':'02','proName':'2G套餐--全国漫游通话套餐46元'}]}
        if('3G'==type){params.ProType=[{'proId':'03','proName':'3G套餐--省内流量不限时套餐66元'},{'proId':'04','proName':'3G套餐--国内流量套餐86元'}]}
        if('4G'==type){params.ProType=[
          {'proId':'05','proName':'01-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'02-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'03-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'04-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'05-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'06-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'07-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'08-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'09-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'10-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'11-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'12-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'13-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'14-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'15-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'16-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'17-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'18-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'19-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'20-4G套餐--省内4G加量不加价126元'},
          {'proId':'05','proName':'21-4G套餐--4G畅游互联网套餐106元'}, {'proId':'06','proName':'22-4G套餐--省内4G加量不加价126元'}
        ]}
        return params.ProType;*/
        //查询产品
        return appItfAopService.do_queryProductList_req(param);
      },
      checkperson:function(param){
        return appItfAopService.do_validCust_req(param);
      },
      //查找号码
      querySerialNumberList:function(param){
        return appItfAopService.do_querySerialNumberList_req(param);
      },
      //校验手机号是否可以返档
      validMobilePhone:function(param)
      {
        return appItfAopService.do_searchUserInfoByPhone_req(param);
      },

      //手机号和SIM卡号互查
      queryUserBySim:function(params)
      {

        return appItfAopService.do_queryUserBySim_req(params);
      },
      validSerialNumber2G:function(param){
        return appItfAopService.do_validSerialNumber2G_req(param);
      },
      validSerialNumber4G:function(param){
        return appItfAopService.do_validSerialNumber4G_req(param);
      },
      //查找发展人信息
      queryDevelopList:function(param){
        return appItfAopService.do_queryDevelopList_req(param);
      },
      ReadIccid:function(param,success,error){
        bluetooth.sr_iccid(param,success,error);
      },
      /*readSimCard:function(param){
        bluetooth.sr_iccid(param,function(success){
          $timeout(function() {
            params.BlueSimCard = angular.fromJson(success);
          },1000);
        },function(error){
          var alertPopup=$ionicPopup.alert({
            title:'读取失败',
            template:angular.toJson(error),
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        })},*/
      //活动页面的下拉列表级联
      changeHuoDong:function(type){
        if('01'==type){params.HuoList=[{"hlid":"03","hlname":"2G爽翻天"}]}
        if('02'==type){params.HuoList=[{"hlid":"02","hlname":"3G爽翻天"}]}
        if('03'==type){params.HuoList=[{"hlid":"01","hlname":"4G爽翻天"}]}
        return params.HuoList;
      },
      //step2的下拉刷新
      doProfresh:function(){
        params.PagePro=1;
        params.PromoreData=true;
      },
      //step2的上拉加载
      /*doProLoadMore:function(){
        params.PagePro++;

        return getProType('4G').then(function(data){alert(data)},function(error){alert(error)});
      },*/
      /**
       * 下拉刷新
       */
      doNumRefresh:function(){
        params.PageNum=1;
        params.NummoreData=true;
       /* return AppItfService.do_Z30005_req(params).then(function(data){
            //angular.copy(data.Response.mdnInfoList, params.mdninfoList);
            params.mdnRecord=data.Response.mdnInfoList.mdnInfo;
            console.log("查询号码："+angular.toJson(params.mdnRecord));
          },
          function(error){
            console.log(angular.toJson(error));
          });*/
        params.PhoneNumList=[
          {"id":"01","pname":"a1234567891"}, {"id":"02","pname":"b1234567891"}, {"id":"03","pname":"c1234567891"},
          {"id":"04","pname":"d1234567891"},{"id":"05","pname":"e1234567891"}, {"id":"06","pname":"f1234567891"},
          {"id":"07","pname":"g1234567891"},{"id":"08","pname":"h1234567891"},{"id":"09","pname":"i1234567891"}
        ];
        return params;
      },
      //提交订单
      do_createOrder_req:function(param){
        if(params.val_tclx=="4G"){
          return  appItfAopService.do_createOrder4G_req(param);
        }else if(params.val_tclx=="2G"){
          return  appItfAopService.do_createOrder2G_req(param);
        }

      },
      //插入日志
      insertLog:function(param)
      {
        return appItfAopService.do_insertLog_req(param);
      }
    }

  }]);
